package cn.itcast.bos.web.action.base;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.IfixedAreaService;
import cn.itcast.crm.domain.Customer;

@Controller
@Namespace("/")
@Scope("prototype")
@ParentPackage("json-default")


public class Fxed_AreaAction implements ModelDriven<FixedArea>  {
	

	
	private FixedArea fixedArea=new FixedArea();
	@Autowired
	private IfixedAreaService IfixedAreaService;
	
	@Override
	public FixedArea getModel() {
		// TODO Auto-generated method stub
		return fixedArea;
	}
	
	public int getPage() {
		return page;
	}



	public void setPage(int page) {
		this.page = page;
	}



	public int getRows() {
		return rows;
	}



	public void setRows(int rows) {
		this.rows = rows;
	}

	private int page;

	private int rows;
	
	
	private String CustomerIds;
	
	public void setCustomerIds(String customerIds) {
		CustomerIds = customerIds;
	}

	@Action(value = "fixedArea_pageQuery"/*, results = { @Result(name = "success", type = "json") }*/)
	public void fixedArea_pageQuery(){
		
		Pageable pageable = new PageRequest(page - 1, rows);
		
		Page<FixedArea> pages= IfixedAreaService.findAll(pageable);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", pages.getTotalElements());
		map.put("rows", pages.getContent());
	/*	ServletActionContext.getContext().getValueStack().push(map);*/
		String jsonString = JSON.toJSONString(map);
		try {
			ServletActionContext.getResponse().setCharacterEncoding("utf-8");
			ServletActionContext.getResponse().getWriter().write(jsonString);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	


	@Action(value = "fixedArea_findNoAssociationCustomers")
	public void fixedArea_findNoAssociationCustomers(){
		Collection<? extends Customer> collection = WebClient.create("http://localhost:9002/webservices/services/customerService/CusermerService/null").accept("application/json").getCollection(Customer.class);
		String jsonString = JSON.toJSONString(collection);
		try {
			ServletActionContext.getResponse().setCharacterEncoding("utf-8");
			ServletActionContext.getResponse().getWriter().write(jsonString);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	
	}
	
	
	@Action(value="fixedArea_findHasAssociationFixedAreaCustomers")
	public void fixedArea_findHasAssociationFixedAreaCustomers(){
		Collection<? extends Customer> collection = WebClient.create("http://localhost:9002/webservices/services/customerService/CusermerService/fixedAreaId?id1="+fixedArea.getId()).accept("application/json").getCollection(Customer.class);
		String jsonString = JSON.toJSONString(collection);
		try {
			ServletActionContext.getResponse().setCharacterEncoding("utf-8");
			ServletActionContext.getResponse().getWriter().write(jsonString);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@Action(value="fixedArea_associationCustomersToFixedArea", results = { @Result(name = "success", type = "redirect", location = "./pages/base/fixed_area.html") })
	public String  fixedArea_associationCustomersToFixedArea(){
		System.out.println(CustomerIds);
		String c=CustomerIds.replace(" ","");
      System.out.println("周永健"+"http://localhost:9002/webservices/services/customerService/CusermerService/update?CustomerIds="+c+"&fixedAreaId="+fixedArea.getId());
		   WebClient.create("http://localhost:9002/webservices/services/customerService/CusermerService/update?CustomerIds="+c+"&fixedAreaId="+fixedArea.getId()).put(null);
	/*	 WebClient.create("http://localhost:9002/webservices/services/customerService/CusermerService/update?CustomerIds=1&fixedAreaId=1").put(null);*/
		return "success";
	}


}
